<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="项目名称" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="所属分类" prop="productCategoryId">
        <el-select v-model="queryParams.productCategoryId" filterable placeholder="所属分类" clearable size="mini">
          <el-option v-for="item in productCategoryOptions" :key="item.id" :label="item.name"
                     :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否禁用" prop="status">
        <el-select v-model="queryParams.status" filterable placeholder="商品状态" clearable size="mini">
          <el-option v-for="dict in statusOptions" :key="dict.value" :label="dict.label" :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否上架" prop="publishStatus">
        <el-select v-model="queryParams.publishStatus" filterable placeholder="是否上架" clearable size="mini">
          <el-option v-for="dict in pulishStatusOptions" :key="dict.value" :label="dict.label" :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否新品" prop="newStatus">
        <el-select v-model="queryParams.newStatus" filterable placeholder="是否新品" clearable size="mini">
          <el-option v-for="dict in newStatusOptions" :key="dict.value" :label="dict.label" :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐" prop="recommendStatus">
        <el-select v-model="queryParams.recommendStatus" filterable placeholder="是否推荐" clearable size="mini">
          <el-option v-for="dict in recommendStatusOptions" :key="dict.value" :label="dict.label"
                     :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="verifyStatus">
        <el-select v-model="queryParams.verifyStatus" filterable placeholder="审核状态" clearable size="mini">
          <el-option v-for="dict in verifyStatusOptions" :key="dict.value" :label="dict.label"
                     :value="+dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期"
                        end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPerm="['beauty:product:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPerm="['beauty:product:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPerm="['beauty:product:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" :height="tableHeight">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column type="expand">
        <template slot-scope="{row}">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="副标题:">
              <span>{{ row.subTitle }}</span>
            </el-form-item>
            <el-form-item label="所属品牌:">
              <span>{{ row.brandName }}</span>
            </el-form-item>
            <el-form-item label="描述:">
              <span>{{ row.pdesc }}</span>
            </el-form-item>
            <el-form-item label="促销价格:">
              <span>{{ row.promotionPrice }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="序号" type="index" width="50" align="center"/>
      <el-table-column label="图片" align="center" prop="pic" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.pic==''">暂无</span>
          <el-image v-else :src="scope.row.pic" style="width: 60px; height: 60px" fit="fill"
                    :preview-src-list="[scope.row.pic]"/>
        </template>
      </el-table-column>
      <el-table-column prop="albumPics" align="center" label="轮播图" width="120px">
        <template slot-scope="{row}">
          <div class="carousel">
            <el-carousel height="60px" arrow="never" indicator-position="none">
              <el-carousel-item v-for="item in row.albumPics.split(',')" :key="item">
                <el-image style="width: 100px; height: 60px" :src="item" fit="fill"/>
              </el-carousel-item>
            </el-carousel>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="分类" align="center" prop="productCategoryId" :show-overflow-tooltip="true"
                       :formatter="productCategoryFormat"/>
      <el-table-column label="标签" width="120px">
        <template slot-scope="{row}">
          <div class="switch-group">
            <div class="switch-item">
              <span>禁用：</span>
              <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
                         @change="handleStatusChange(row)"
                         active-color="#6461C5"/>
            </div>
            <div class="switch-item">
              <span>上架：</span>
              <el-switch v-model="row.publishStatus" :active-value="1" :inactive-value="0"
                         @change="handlePublisStatusChange(row)"/>
            </div>
            <div class="switch-item">
              <span>新品：</span>
              <el-switch v-model="row.newStatus" :active-value="1" :inactive-value="0"
                         @change="handleNewStatusChange(row)" active-color="#ff4949"/>
            </div>
            <div class="switch-item">
              <span>推荐：</span>
              <el-switch v-model="row.recommendStatus" :active-value="1" :inactive-value="0"
                         @change="handleRecommendStatusChange(row)" active-color="#13ce66"/>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" :show-overflow-tooltip="true"/>
      <el-table-column label="购买可获取积分" align="center" prop="point" :show-overflow-tooltip="true"/>
      <el-table-column label="销量" align="center" prop="sale" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="gmtCreate" width="100">
        <template slot-scope="{row}">
          <span>{{ parseTime(row.gmtCreate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPerm="['beauty:product:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPerm="['beauty:product:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  changeNewStatus,
  changeStatus,
  changePublishStatus,
  changeRecommandStatus,
  delProduct,
  listProduct
} from '@a/beauty/product'
import { getProductCategoryOptionSelect, getSecondLevelProductCategories } from '@a/beauty/productCategory'

export default {
  name: 'Product',
  data() {
    return {
      tableHeight: window.innerHeight - 360,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否审核数据字典
      verifyStatusOptions: [],
      // 是否推荐
      recommendStatusOptions: [],
      // 是否新品
      newStatusOptions: [],
      // 是否上架
      pulishStatusOptions: [],
      // 商品状态
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        productCategoryId: undefined,
        name: undefined,
        status: undefined,
        publishStatus: undefined,
        newStatus: undefined,
        recommendStatus: undefined,
        verifyStatus: undefined
      },
      // 商品分类 id name
      productCategoryOptions: []
    }
  },
  watch: {
    $route() {
      // 跳转到该页面后需要进行的操作
      this.getList()
    }
  },
  created() {
    this.getList()
    this.getDictItems('beauty_product_verify_status').then(response => {
      this.verifyStatusOptions = response.data
    })
    this.getDictItems('beauty_product_status').then(response => {
      this.statusOptions = response.data
    })
    this.getDictItems('beauty_product_publish_status').then(response => {
      this.pulishStatusOptions = response.data
    })
    this.getDictItems('beauty_product_new_status').then(response => {
      this.newStatusOptions = response.data
    })
    this.getDictItems('beauty_product_recommand_status').then(response => {
      this.recommendStatusOptions = response.data
    })
    this.getProductCategoryOptionSelect()
  },
  methods: {
    // 推荐状态
    handleRecommendStatusChange(row) {
      let text = row.recommendStatus === 0 ? '不推荐' : '推荐'
      this.$confirm('确认要"' + text + '""' + row.name + '"商品吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return changeRecommandStatus(row.id, row.recommendStatus)
      }).then(() => {
        this.msgNoticeSuccess(text + '成功')
      }).catch(function() {
        row.recommendStatus = row.recommendStatus === 0 ? 1 : 0
      })
    },
    // 启用(显示) 状态
    handleStatusChange(row) {
      let text = row.status === 0 ? '启用' : '禁用'
      this.$confirm('确认要"' + text + '""' + row.name + '"商品吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return changeStatus(row.id, row.status)
      }).then(() => {
        this.msgNoticeSuccess(text + '成功')
      }).catch(function() {
        row.status = row.status === 0 ? 1 : 0
      })
    },
    // 刷新商品状态
    handleNewStatusChange(row) {
      let text = row.newStatus === 0 ? '不刷新' : '刷新'
      this.$confirm('确认要"' + text + '""' + row.name + '"商品吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return changeNewStatus(row.id, row.newStatus)
      }).then(() => {
        this.msgNoticeSuccess(text + '成功')
      }).catch(function() {
        row.newStatus = row.newStatus === 0 ? 1 : 0
      })
    },
    // 改变上架状态
    handlePublisStatusChange(row) {
      let text = row.publishStatus === 0 ? '下架' : '上架'
      this.$confirm('确认要"' + text + '""' + row.name + '"商品吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return changePublishStatus(row.id, row.publishStatus)
      }).then(() => {
        this.msgNoticeSuccess(text + '成功')
      }).catch(function() {
        row.publishStatus = row.publishStatus === 0 ? 1 : 0
      })
    },
    // 获取商品分类列表
    getProductCategoryOptionSelect() {
      getSecondLevelProductCategories().then(response => {
        this.productCategoryOptions = response.data
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      listProduct(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 字典翻译
    productCategoryFormat(row, column) {
      return this.selectOptionName(this.productCategoryOptions, row.productCategoryId)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 跳转到添加页
      this.$router.push({ name: 'productAdd' })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // 跳转到修改页
      this.$router.push({ name: 'productUpdate', params: { productId: row.id } })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delProduct(ids)
      }).then(() => {
        this.getList()
        this.msgNoticeSuccess('删除成功')
      })
    }
  }
}
</script>
<style scoped>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.up-img-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #000000;
  background-color: #e8e8e8;
  width: 170px;
  height: 170px;
  line-height: 170px;
  text-align: center;
}

.up-img {
  width: 170px;
  height: 170px;
  display: block;
}

</style>
